﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Microsoft.Practices.Unity;
using Lekker.Services.Recipes;

namespace Lekker.Web.Mvc.Controllers {
    public class RecipeController : Controller {

        [Dependency]
        public IRecipeService RecipeService { get; set; }


        public ActionResult Index() {

            ViewData["Recipes"] = this.RecipeService.GetRecipes();

            return View();
        }

        public ActionResult View(int id) {

            var recipe = this.RecipeService.GetRecipe(id);

            return View(recipe);
        }

        public ActionResult Save() {

            var id = int.Parse(this.Request.Form["Id"]);
            var recipe = this.RecipeService.GetRecipe(id);

            try {
                
                UpdateModel(recipe, new[] {"Name", "Description"});

                this.RecipeService.UpdateRecipe(recipe);

                return RedirectToAction("Index");

            } catch {
                return View("View", recipe);
            }
 
        }
    }
}
